'use strict';

angular.module('tvshowsApp')
    .controller('ShowController', function ($scope, Show) {
        $scope.shows = [];
        $scope.days = [
            {name: 'Monday'},
            {name: 'Tuesday'},
            {name: 'Wednesday'},
            {name: 'Thursday'},
            {name: 'Friday'},
            {name: 'Saturday'},
            {name: 'Sunday'}];

        $scope.loadAll = function() {
            Show.query(function(result) {
               $scope.shows = result;
            });
        };
        //$scope.clear();
        $scope.loadAll();


        $scope.create = function () {
            Show.save($scope.show,
                function () {
                    $scope.loadAll();
                    $('#saveShowModal').modal('hide');
                    $scope.clear();
                });
        };

        $scope.update = function (id) {
            Show.get({id: id}, function(result) {
                $scope.show = result;
                $scope.show.schedule=result.schedule;
                $('#saveShowModal').modal('show');
            });
        };

        $scope.delete = function (id) {
            Show.get({id: id}, function(result) {
                $scope.show = result;
                $('#deleteShowConfirmation').modal('show');
            });
        };

        $scope.confirmDelete = function (id) {
            Show.delete({id: id},
                function () {
                    $scope.loadAll();
                    $('#deleteShowConfirmation').modal('hide');
                    $scope.clear();
                });
        };

        $scope.clear = function () {
            $scope.show = {name: null, seasonNumber: null, lastEpisodeNumber: null, schedule: 'Select a day', id: null};
        };


        $scope.isScheduleValid = function () {
            if ($scope.show){
                return ($scope.show.schedule!="Select a day");
            }
            return false;
        };

    });
